package com.fc.test.common.websocket;

import com.fc.test.common.websocket.manager.OnLineUserManager;
import com.fc.test.common.websocket.pojo.SocketUser;
import com.fc.test.common.websocket.pojo.message.ToServerTextMessage;
import com.fc.test.common.websocket.sender.MessageSender;
import com.fc.test.common.websocket.util.LayIMFactory;

import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/websocket/{uid}")
@Component
public class WebSocketServer {
    //接收sid
    private String uid="";
    @OnOpen
    public  void open(Session session, @PathParam("uid") String uid){
        SocketUser user = new SocketUser();
        user.setSession(session);
        user.setUserId(uid);
        this.uid=uid;
        //保存在线列表
        LayIMFactory.createManager().addUser(user);
        print("当前在线用户："+LayIMFactory.createManager().getOnlineCount());
        print("缓存中的用户个数："+new OnLineUserManager().getOnLineUsers().size());
    }

    @OnMessage
    public void receiveMessage(String message,Session session){
        //try {

        ToServerTextMessage toServerTextMessage =
                LayIMFactory.createSerializer().toObject(message,ToServerTextMessage.class);

        //得到接收的对象
        MessageSender sender = new MessageSender();

        sender.sendMessage(toServerTextMessage);

        //}catch (Exception e){
        //  e.printStackTrace();
        //}
    }

    @OnError
    public void error(Throwable t) {
        print(t.getMessage());
    }

    @OnClose
    public void close(Session session){

        SocketUser user = new SocketUser();
        user.setSession(session);
        user.setUserId(this.uid);
        print("用户掉线");
        //移除该用户
        LayIMFactory.createManager().removeUser(user);
        //print("当前在线用户："+LayIMFactory.createManager().getOnlineCount());

    }

    private void print(String msg){
        System.out.println(msg);
    }
}
